﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;

namespace HotelManagement._01.FORM
{
    public partial class F51_PaymentInformation : Form
    {
        public F51_PaymentInformation()
        {

            InitializeComponent();
            Init();
            LoadDataToGrid();
            if (AssetsGS.instance.RoleUser == "User") btnDelete.Enabled = false;
        }

        Payment m_Payment;
        PaymentInfoBUS m_PaymentInfoBUS;

        private void Init()
        {
            m_Payment = new Payment();
            m_PaymentInfoBUS = new PaymentInfoBUS();
        }
        private void LoadDataToGrid()
        {
            m_dgv_Payment.DataSource = m_PaymentInfoBUS.ViewPayment();
        }
        private void SetDataToForm(Payment ip_Payment)
        {
            CustomerBUS v_CustomerBUS = new CustomerBUS();
            DataTable v_data=new DataTable();
            v_data = v_CustomerBUS.ViewCustomerById(ip_Payment.CustomerID);
            m_txt_CustomerID.Text = ip_Payment.CustomerID.ToString();
            m_txt_CustomerName.Text = v_data.Rows[0]["CustomerName"].ToString(); ;
            m_txt_PaymentID.Text = ip_Payment.PaymentID.ToString();
            m_txt_Note.Text = ip_Payment.Note;
            m_txt_CheckInID.Text = ip_Payment.CheckInID.ToString();
            m_txt_TotalCost.Text = ip_Payment.TotalCost.ToString();
            m_txt_InvoidNumber.Text = ip_Payment.InvoidNumber.ToString();
            m_dt_PaymentDate.Text = ip_Payment.PaymentDate.ToString();
        }
        private void ResetText()
        {
            m_txt_CustomerID.Text = "";
            m_txt_CustomerName.Text = "";
            m_txt_PaymentID.Text = "";
            m_txt_InvoidNumber.Text = "";
            m_txt_Note.Text = "";
            m_txt_TotalCost.Text = "";
            m_txt_CheckInID.Text = "";
            //m_txt_Keyword.Enabled = true;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (m_txt_PaymentID.Text.Trim() != "") return;
            try 
	        {   
                if (m_PaymentInfoBUS.AddPayment(m_Payment)) MessageBox.Show("Thêm thanh toán thành công");
	        }
	        catch (Exception v_e)
	        {
		        throw v_e;
	        }
            ResetText();
            LoadDataToGrid();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            if (m_txt_PaymentID.Text.Trim() == "")
            {
                MessageBox.Show("Hãy chọn 1 hóa đơn thanh toán!");
            }
            else
            {
                try
                {
                    m_Payment.Note = m_txt_Note.Text.ToString();
                    m_Payment.InvoidNumber = m_txt_InvoidNumber.Text.ToString();
                    m_Payment.PaymentDate = DateTime.Parse(m_dt_PaymentDate.Text.ToString());
                    m_Payment.TotalCost = int.Parse(m_txt_TotalCost.Text.ToString().Trim());
                    if (m_PaymentInfoBUS.UpdatePayment(m_Payment)) MessageBox.Show("Sửa thanh toán thành công");
                }
                catch (Exception v_e)
                {
                    throw v_e;
                }
                ResetText();
                LoadDataToGrid();
            }
        }

        private void m_dgv_Payment_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_Payment.PaymentID =int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["PaymentID"].Value.ToString());
                m_Payment.CustomerID = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString());
                m_Payment.InvoidNumber = m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["InvoidNumber"].Value.ToString();
                m_Payment.PaymentDate = DateTime.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["PaymentDate"].Value.ToString());
                m_Payment.TotalCost = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["TotalCost"].Value.ToString());
                m_Payment.CheckInID = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckInID"].Value.ToString());
                m_Payment.InvoidNumber = m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["InvoidNumber"].Value.ToString();
                m_Payment.PaymentDate = DateTime.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["PaymentDate"].Value.ToString());
                SetDataToForm(m_Payment);
            }
            catch (Exception)
            {

                MessageBox.Show("Bạn chưa chọn loại dịch vụ nào !");
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (m_txt_PaymentID.Text.Trim() == "") return;
            if (MessageBox.Show("Bạn có chắc là bạn muốn xóa thông tin này ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (m_PaymentInfoBUS.DeletePayment(m_Payment)) MessageBox.Show("Đã xóa thành công !"); else MessageBox.Show("Lỗi không thể xóa !");
                LoadDataToGrid();
            }
            ResetText();
        }

        private void Reset(object sender, EventArgs e)
        {
            ResetText();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }


        //internal PaymentInfoBUS m_PaymentInfoBUS { get; set; }
    }
}
